/*
 * Filename    : Scene_Playing.java
 * Projectname : SoiScene
 * Date        : Jan 9, 2013 , 10:47:27 AM 
 * 
 */

package id.pratama.soiscene.scene;

import java.io.IOException;

import id.pratama.soiscene.IManageableScene;
import id.pratama.soiscene.Utils;
import id.pratama.soiscene.config.IApps_Data;
import id.pratama.soiscene.config.IApps_Define;
import id.pratama.soiscene.data.IGraphic_Data;
import id.pratama.soiscene.data.ISound_Data;

import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.entity.scene.IOnAreaTouchListener;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.input.touch.detector.ClickDetector.IClickDetectorListener;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.util.color.Color;

public class Scene_Playing extends Scene implements IManageableScene, IApps_Data, IApps_Define, ISound_Data,
		IOnAreaTouchListener, IGraphic_Data, IClickDetectorListener, IOnSceneTouchListener
{
	BaseGameActivity			activity;

	private ClickDetector		clickDetector;

	private Sound[]				snd_angklung	= new Sound[SND_ANGKLUNG.length];
	private Sound[]				snd_gamelan		= new Sound[SND_GAMELAN.length];
	private Text[]				txtAngka;
	private BitmapTextureAtlas	tex_Kotak;
	private TextureRegion		reg_Kotak;
	private Sprite[]			spr_Kotak;

	public Scene_Playing(BaseGameActivity pActivity)
	{
		// TODO Auto-generated constructor stub
		activity = pActivity;
		clickDetector = new ClickDetector(this);

		loadResources();
		applyResources();
		registerUpdateHandler();
	}

	@Override
	public void loadResources()
	{
		// TODO Auto-generated method stub

		tex_Kotak = new BitmapTextureAtlas(activity.getTextureManager(), KOTAK_TEX_WIDTH, KOTAK_TEX_HEIGHT,
				Utils.getTextureOption());
		reg_Kotak = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(tex_Kotak, activity.getAssets(), KOTAK, 0, 0);

		tex_Kotak.load();

		switch (Scene_Alat_Musik.loadedSound)
		{
			case Scene_Alat_Musik.LOAD_ANGKLUNG:
				txtAngka = new Text[SND_ANGKLUNG.length];
				for (int i = 0; i < SND_ANGKLUNG.length; i++)
				{
					try
					{
						snd_angklung[i] = SoundFactory.createSoundFromAsset(activity.getSoundManager(),
								activity.getApplicationContext(), SND_ANGKLUNG[i]);
					} catch (IOException e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					// txtAngka[i] = new Text(0, 0, MainActivity.mFont, "" + (i
					// + 1),
					// activity.getVertexBufferObjectManager());
					// registerTouchArea(txtAngka[i]);
				}
				break;
			case Scene_Alat_Musik.LOAD_GAMELAN:
				txtAngka = new Text[SND_GAMELAN.length];
				for (int i = 0; i < SND_GAMELAN.length; i++)
				{
					try
					{
						snd_gamelan[i] = SoundFactory.createSoundFromAsset(activity.getSoundManager(),
								activity.getApplicationContext(), SND_GAMELAN[i]);
					} catch (IOException e)
					{
						// TODO: handle exception
					}

					// txtAngka[i] = new Text(0, 0, MainActivity.mFont, "" + (i
					// + 1),
					// activity.getVertexBufferObjectManager());
					// registerTouchArea(txtAngka[i]);
				}
				break;

		}

	}

	@Override
	public void applyResources()
	{
		setBackground(new Background(Color.WHITE));
		// TODO Auto-generated method stub

		switch (Scene_Alat_Musik.loadedSound)
		{
			case Scene_Alat_Musik.LOAD_ANGKLUNG:
				spr_Kotak = new Sprite[SND_ANGKLUNG.length];
				for (int i = 0; i < SND_ANGKLUNG.length; i++)
				{
					spr_Kotak[i] = new Sprite(0, 0, Utils.getRatioW(KOTAK_WIDTH), Utils.getRatioH(KOTAK__HEIGHT),
							reg_Kotak, activity.getVertexBufferObjectManager());
					attachChild(spr_Kotak[i]);
					spr_Kotak[i].setPosition(Utils.getRatio(75) * i, RES_HEIGHT / 2 - spr_Kotak[i].getHeight() / 2);
					registerTouchArea(spr_Kotak[i]);

					// attachChild(txtAngka[i]);
					// txtAngka[i].setPosition(Utils.getRatio(100) * i,
					// RES_HEIGHT / 2 - txtAngka[i].getHeight() / 2);
				}
				break;
			case Scene_Alat_Musik.LOAD_GAMELAN:
				spr_Kotak = new Sprite[SND_GAMELAN.length];
				for (int i = 0; i < SND_GAMELAN.length; i++)
				{
					spr_Kotak[i] = new Sprite(0, 0, Utils.getRatioW(KOTAK_WIDTH), Utils.getRatioH(KOTAK__HEIGHT),
							reg_Kotak, activity.getVertexBufferObjectManager());
					attachChild(spr_Kotak[i]);
					spr_Kotak[i].setPosition(Utils.getRatio(75) * i, RES_HEIGHT / 2 - spr_Kotak[i].getHeight() / 2);
					registerTouchArea(spr_Kotak[i]);

					// attachChild(txtAngka[i]);
					// txtAngka[i].setPosition(Utils.getRatio(100) * i,
					// RES_HEIGHT / 2 - txtAngka[i].getHeight() / 2);
				}

				break;

		}

	}

	@Override
	public void registerUpdateHandler()
	{
		// TODO Auto-generated method stub
		setOnAreaTouchListener(this);

	}

	@Override
	public void unloadResources()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX,
			float pTouchAreaLocalY)
	{
		// TODO Auto-generated method stub
		for (int i = 0; i < SND_ANGKLUNG.length; i++)
		{
			if (pTouchArea.equals(spr_Kotak[i]))
			{
				switch (Scene_Alat_Musik.loadedSound)
				{
					case Scene_Alat_Musik.LOAD_ANGKLUNG:
						snd_angklung[i].play();
						break;
					case Scene_Alat_Musik.LOAD_GAMELAN:
						snd_gamelan[i].play();
						break;

				}

			}
		}
		return false;
	}

	@Override
	public void onClick(ClickDetector pClickDetector, int pPointerID, float pSceneX, float pSceneY)
	{
		// TODO Auto-generated method stub
		for (int i = 0; i < SND_ANGKLUNG.length; i++)
		{
			
		}

	}

	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent)
	{
		// TODO Auto-generated method stub
		clickDetector.onTouchEvent(pSceneTouchEvent);
		
		return false;
	}

}
